package com.chigga.common.core.domain;

import com.chigga.common.constants.HttpStatusConstant;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * 全局统一响应
 * @param <T> 响应数据类型
 * @author DefChigga
 */
@Schema(description = "全局统一响应结果对象")
@Data
public class ResponseResult<T> implements Serializable {
    /**
     * 序列化标识
     */
    @Serial
    private static final long serialVersionUID = 1L;
    /**
     * 响应成功标记
     */
    @Schema(description = "响应成功标记")
    private Boolean success;
    /**
     * 响应状态码
     */
    @Schema(description = "响应状态码")
    private Integer code;
    /**
     * 响应消息
     */
    @Schema(description = "响应消息")
    private String message;
    /**
     * 响应数据
     */
    @Schema(description = "响应数据")
    private T data;
    /**
     * 时间戳
     */
    @Schema(description = "时间戳")
    private Long timestamp;

    /**
     * 静态方法私有化，防止外部使用 new 创建
     */
    private ResponseResult() {}

    /**
     * 创建响应对象
     *
     * @param data    响应数据
     * @param code    状态码
     * @param message 响应消息
     * @return ResponseResult
     */
    private static <T> ResponseResult<T> createResponse(T data, Integer code, String message) {
        ResponseResult<T> result = new ResponseResult<>();
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(isSuccess(code));
        result.setTimestamp(System.currentTimeMillis());
        return result;
    }

    /**
     * 成功响应
     */
    public static <T> ResponseResult<T> success() {
        return createResponse(null, HttpStatusConstant.SUCCESS, "操作成功");
    }

    /**
     * 成功响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(T data) {
        return createResponse(data, HttpStatusConstant.SUCCESS, "操作成功");
    }

    /**
     * 成功响应
     *
     * @param data    响应数据
     * @param message 响应消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> success(T data, String message) {
        return createResponse(data, HttpStatusConstant.SUCCESS, message);
    }

    /**
     * 创建成功响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successCreated() {
        return createResponse(null, HttpStatusConstant.CREATED, "创建成功");
    }

    /**
     * 创建成功响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successCreated(T data) {
        return createResponse(data, HttpStatusConstant.CREATED, "创建成功");
    }
    /**
     * 创建成功响应
     *
     * @param data 响应数据
     * @param message 响应消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successCreated(T data, String message) {
        return createResponse(data, HttpStatusConstant.CREATED, message);
    }


    /**
     * 更新成功响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successUpdated() {
        return createResponse(null, HttpStatusConstant.SUCCESS, "更新成功");
    }

    /**
     * 更新成功响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successUpdated(T data) {
        return createResponse(data, HttpStatusConstant.SUCCESS, "更新成功");
    }

    /**
     * 删除成功响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successDeleted() {
        return createResponse(null, HttpStatusConstant.DELETED, "删除成功");
    }

    /**
     * 删除成功响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successDeleted(T data) {
        return createResponse(data, HttpStatusConstant.DELETED, "删除成功");
    }

    /**
     * 查询成功响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> successSelected(T data) {
        return createResponse(data, HttpStatusConstant.SUCCESS, "查询成功");
    }

    /**
     * 失败响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failure() {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, "操作失败");
    }

    /**
     * 失败响应
     *
     * @param message 响应消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failure(String message) {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, message);
    }

    /**
     * 失败响应
     *
     * @param code    状态码
     * @param message 响应消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failure(Integer code, String message) {
        return createResponse(null, code, message);
    }

    /**
     * 创建失败响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureCreated() {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, "创建失败");
    }

    /**
     * 创建失败响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureCreated(T data) {
        return createResponse(data, HttpStatusConstant.INTERNAL_SERVER_ERROR, "创建失败");
    }

    /**
     * 创建失败响应
     *
     * @param data 响应数据
     * @param message 响应消息
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureCreated(T data, String message) {
        return createResponse(data, HttpStatusConstant.INTERNAL_SERVER_ERROR, message);
    }

    /**
     * 更新失败响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureUpdated() {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, "更新失败");
    }

    /**
     * 更新失败响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureUpdated(T data) {
        return createResponse(data, HttpStatusConstant.INTERNAL_SERVER_ERROR, "更新失败");
    }

    /**
     * 删除失败响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureDeleted() {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, "删除失败");
    }

    /**
     * 删除失败响应
     *
     * @param data 响应数据
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureDeleted(T data) {
        return createResponse(data, HttpStatusConstant.INTERNAL_SERVER_ERROR, "删除失败");
    }

    /**
     * 查询失败响应
     *
     * @return ResponseResult
     */
    public static <T> ResponseResult<T> failureSelected() {
        return createResponse(null, HttpStatusConstant.INTERNAL_SERVER_ERROR, "查询失败");
    }

    /**
     * 判断响应是否成功
     *
     * @param code 响应状态码
     * @return boolean
     */
    public static Boolean isSuccess(Integer code) {
        return code == HttpStatusConstant.SUCCESS || code == HttpStatusConstant.CREATED || code == HttpStatusConstant.DELETED;
    }

    /**
     * 判断响应是否失败
     *
     * @param code 响应状态码
     * @return boolean
     */
    public static Boolean isFailure(Integer code) {
        return !isSuccess(code);
    }
}
